﻿using Microsoft.Ted.Wacel.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Microsoft.Ted.Wacel.Redis
{
    public class Dictionary<T> : Base.Dictionary<byte[]>
    {
        public Dictionary(string name, string hostName, int port = 6379, string password = null)
            : base(name,
                    new RedisCacheProvider(new BookSleeve.RedisConnection(hostName, port, password: password, allowAdmin: true), name), null)
        {
        }
        public new T this[string key]
        {
            get
            {

                var byteData = base[key];
                if (byteData == null)
                    return default(T);
                else
                    return (T)deserializeObject(typeof(T), byteData);
            }
            set
            {
                base[key] = serializeObject(value);
            }
        }
        private byte[] serializeObject(object obj)
        {
            return JsonSerializer.Serialize(obj.GetType(), obj);
        }
        private object deserializeObject(Type type, byte[] data)
        {
            return JsonSerializer.Deserialize(type, data);
        }
    }
}
